package servlets;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Data.Declarations;

/**
 * Servlet that will create the download operation to the file in the server shared 
 * folder
 */

public class DownloadFile extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DownloadFile() {
        super();
        
        // TODO Auto-generated constructor stub
    }

    /** neither in get or post method, we will get the file name from the request and send 
     * the String to doDownload method
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */

    public void doGet(HttpServletRequest request, HttpServletResponse response)throws 
    ServletException, IOException{ 
    	String filename = request.getParameter("filename").toString();
    	doDownload(request,response,filename);
    }
    


    /** neither in get or post method, we will get the file name from the request and send 
         * the String to doDownload method
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String filename = request.getParameter("filename").toString();
    	doDownload(request,response,filename);
	}
	
    /**
     * this function creates a connection with the http request and move the file through the http response 
     * @param req the http request from the page that tries to connect to this servlet
     * @param resp the http response from the page that tries to connect to this servlet
     * @param filename the file name to be downloaded
     */

	private void doDownload( HttpServletRequest req, HttpServletResponse resp,
            String filename)
	
	{
		try{
				/*
			resp.setContentType("text/html;charset=UTF-8");
	        PrintWriter out = resp.getWriter();
	        out.println("<html>");
	    	  out.println("<head>");
	    	  out.println("<title>DropBox Server</title>");
	    	  out.println("</head>");
	      	  out.println("<body>");
	      	  out.println("<H1>"+filename+"</H1>");
	      	  out.println("</body>");
	      	  out.println("</body>");
	      	out.println("</html>");
	      	*/
			String fullPath=Declarations.instance.getServerFiles()+System.getProperty("file.separator")+filename;
			
			File                f        = new File(fullPath);
			int                 length   = 0;
			ServletOutputStream op       = resp.getOutputStream();
			ServletContext      context  = getServletConfig().getServletContext();
			String              mimetype = context.getMimeType( filename );
			
			//
			//  Set the response and go!
			//
			//
			resp.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
			resp.setContentLength( (int)f.length() );
			resp.setHeader( "Content-Disposition", "attachment; filename=\"" + filename);
			System.out.println(fullPath);
			//
			//  Stream to the requester.
			//
			byte[] bbuf = new byte[1024];
			DataInputStream in = new DataInputStream(new FileInputStream(f));
			
			while ((in != null) && ((length = in.read(bbuf)) != -1))
			{
			op.write(bbuf,0,length);
			}
			
			in.close();
			op.flush();
			op.close();
		}
		catch(IOException e)
		{
			
		}
		
	}
}
